package org.shx.springboot.ollama.ai.controller;

import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.shx.springboot.ollama.ai.service.ChatService;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;


@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
public class ChatController {

    private final ChatService chatService;

    /**
     * 根据消息直接输出回答
     */
    @GetMapping("/chat")
    public ResponseEntity<String> chat(@RequestParam("message") String message) {
        return ResponseEntity.ok(chatService.chat(message));
    }

    /**
     * 根据消息直接输出回答 流式输出
     */
    @GetMapping("/streamChat")
    public ResponseEntity<Flux<ChatResponse>> streamChat(@RequestParam("message") String message) {
        return ResponseEntity.ok(chatService.streamChat(message));
    }

    /*流式输出3*/
    // 方案2：修改返回类型为ResponseEntity<Flux<String>>（更直接）
    @GetMapping(value = "/chat/ai/generateStreamChat", produces = "text/event-stream;charset=UTF-8")
    public ResponseEntity<Flux<String>> generateStreamChat(@RequestParam(name = "message") String message) { // 显式指定参数名，避免之前的参数名问题
        // 显式设置 Content-Type，强化匹配
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType("text/event-stream;charset=UTF-8"))
                .body(chatService.generateStreamChat(message));
    }



    // 会话中存储对话历史的键
    public static final String CHAT_HISTORY_KEY = "chat_history";
    /*流式输出4 带会话记忆-但是需要会话生效 跨域配置中需要配置域名或者ip端口*/
    @GetMapping(value = "/chat/ai/generateStreamChatSession", produces = "text/event-stream;charset=UTF-8")
    public ResponseEntity<Flux<String>> generateStreamChatSession(
            @RequestParam(name = "message") String message,
            HttpSession session) { // 使用HttpSession存储对话历史
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType("text/event-stream;charset=UTF-8"))
                .body(chatService.generateStreamChatSession(message,session));
    }
    // 可选：添加清除历史的接口
    @PostMapping("/chat/clearHistory")
    public ResponseEntity<Void> clearHistory(HttpSession session) {
        session.removeAttribute(CHAT_HISTORY_KEY);
        return ResponseEntity.ok().build();
    }


    /*流式输出--使用文档知识库*/
    @GetMapping(value = "/chat/ai/generateStreamDoc", produces = "text/event-stream;charset=UTF-8")
    public ResponseEntity<Flux<String>> generateStreamDoc(@RequestParam(name = "message") String message) { // 显式指定参数名，避免之前的参数名问题
        Flux<String> sseFlux = chatService.elkStreamChat(message);
        // 显式设置 Content-Type，强化匹配
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType("text/event-stream;charset=UTF-8"))
                .body(sseFlux);
    }
    /*流式输出--使用文档知识库 -- 自定义了会话，带记忆*/
    @GetMapping(value = "/chat/ai/generateStreamDocByMyClient", produces = "text/event-stream;charset=UTF-8")
    public ResponseEntity<Flux<String>> generateStreamDocByMyClient(@RequestParam(name = "message") String message,HttpSession session) { // 显式指定参数名，避免之前的参数名问题
        Flux<String> sseFlux = chatService.elkStreamChatByMyClient(message,session);
        // 显式设置 Content-Type，强化匹配
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType("text/event-stream;charset=UTF-8"))
                .body(sseFlux);
    }

    @GetMapping("/simpleClient")
    public ResponseEntity<Flux<String>> simpleClient(@RequestParam(name = "message") String message) {
        Flux<String> a = chatService.simpleClient(message);
//        return ResponseEntity.ok(a.toString());
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType("text/event-stream;charset=UTF-8"))
                .body(a);
    }
    /*mcp 调用数据库*/
    @GetMapping("/test")
    public ResponseEntity<Flux<String>> mcpClient(@RequestParam(name = "message") String message) {
        Flux<String> a = chatService.mcpClient(message);
//        return ResponseEntity.ok(a.toString());
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType("text/event-stream;charset=UTF-8"))
                .body(a);
    }

}